package com.demo.viewpager;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.provider.ContactsContract;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

public class ViewPagerAdater extends PagerAdapter {

    ArrayList<Drawable> list;
    Context context;
    private ImageView img3;

    public ViewPagerAdater() {
        super();
    }

    public ViewPagerAdater(Context context, ArrayList<Drawable> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
//        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
//        ImageView imageView = new ImageView(context);
//        if (position>list.size()-1) {
//            imageView.setImageDrawable(list.get(0));
//        }else {
//        imageView.setImageDrawable(list.get(position));
//        }
//        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        int newPosition = position % list.size();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_vp, null);
        ImageView img2 = (ImageView) view.findViewById(R.id.img2);
        img3 = (ImageView) view.findViewById(R.id.img3);
        img2.setImageDrawable(list.get(newPosition));
//        if (position < list.size() - 1) {
//            img3.setImageDrawable(list.get(position + 1));
//        } else {
//            img3.setImageDrawable(list.get(0));
//        }


//
        container.addView(view);
        return view;
    }
}
